<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>

    <div style="margin-left: 10px;">
      <el-form ref="form" :rules="rules" label-width="120px">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item prop="code">
            <div style="width: 270px;display: flex;">
              <div style="width: 100px;">工艺编码</div>
              <el-input v-model="queryParams.code" placeholder="请输入工艺编码" clearable @keyup.enter.native="handleQuery" />
            </div>
          </el-form-item>
          <el-form-item prop="name">
            <div style="width: 270px;display: flex;">
              <div style="width: 100px;">工艺名称</div>
              <el-input v-model="queryParams.name" placeholder="请输入工艺名称" clearable @keyup.enter.native="handleQuery" />
            </div>
          </el-form-item>

          <el-form-item prop="shuoming">
            <div style="width: 270px;display: flex;">
              <div style="width: 100px;">工艺说明</div>
              <el-input v-model="queryParams.shuoming" placeholder="请输入工艺说明" clearable
                @keyup.enter.native="handleQuery" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <div style="margin-top: 20px;"></div>
        <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading"  @sort-change="handleSortChange" :height="tableMaxHeight">
          <el-table-column width="55">
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="currentRow">
                {{""}}
              </el-radio>
            </template>
          </el-table-column>
          <el-table-column sortable label="工艺编号" align="center" prop="code"  />
          <el-table-column sortable label="工艺名称" align="center" prop="name"  />
          <el-table-column label="工艺路线" align="center" prop="photo"  />
          <el-table-column label="工艺说明" align="center" prop="shuoming" show-overflow-tooltip />
          <el-table-column align="center" label="工艺文件" prop="file" width="200">
            <template slot-scope="scope">
              <image-preview v-if="scope.row.file" :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>
          <!--<el-table-column align="center" label="工艺图纸" prop="photo" width="200">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.photo" height="60px" width="40px"></image-preview>
                </template>
              </el-table-column>-->
          <!--<el-table-column label="备注" align="center" prop="remark"/>-->
        </el-table>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitGy">确 认</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getAction,
    addAction,
    getById
  } from '@/api/manage'

  export default {
    name: "AddGongyiList",
    components: {},
    data() {
      return {
        title: "工艺列表",
        open: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        rows: [],
        rules: {},
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        currentRow: null,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        saleId: null,
        goodsId: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20000,
          shuoming: null,
          goodsId: null,
          gongyiId: null,
          code: null,
          name: null,
          orderByColumn: "code",
          isAsc: "asc",
          params: {},
        },
        url: {
          list: '/lnGongyi/lnGongyi/list',
          addGyGoods: '/lnGongyigoods/lnGongyigoods/addGyGoods',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
       /** 排序触发事件 */
     handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      showGy(goodsId, saleId) {
        this.goodsId = goodsId;
        this.saleId = saleId;
        this.open = true;
        this.getList();
      },
      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      submitGy() {
        if (this.currentRow == null || this.currentRow == '' || this.currentRow == 'undefined') {
          this.$modal.msgError("请选择一条记录");
        } else {
          let params = {
            "goodsId": this.goodsId,
            "gongyiId": this.currentRow,
            "saleId": this.saleId
          }
          getAction(this.url.addGyGoods, params).then(response => {
            this.$modal.msgSuccess("添加完成");
            this.open = false;
            this.$emit('ok');
          });
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
    }
  };
</script>
